<?php
	require_once '../lib/controller.php';

	class action extends controller
	{
		function do_get()
		{
			$db = $this->module('lib.db');
			$sql = "select id,cname,parent_cname from meye_site_cate";
			$data = $db->query($sql , 'all');
			$sql = "select site_cate_id,count(1) cnt from meye_site group by site_cate_id";
			$data2 = $db->query( $sql );
			$list_data = $this->tool_map( $data, $data2 );

			$site_data = array();
			if( isset($this->get['id']) )
			{
				$sql = "select id,name,url from meye_site where site_cate_id = '" . $this->get['id'] . "' ";
				$site_data = $db->query( $sql );

				$sql = "select cname from meye_site_cate where id = '" . $this->get['id'] . "' ";
				$site_cate_data = $db->query($sql, 'array', true );
			}

			$this->view('www.index', array('list_data'=>$list_data,'site_data'=>$site_data,'title'=>$site_cate_data[0]['cname']) );
		}

		function check_get()
		{
			if( isset($this->get['id']) )
			{
				if( ! is_numeric($this->get['id']) )
				{
					$this->msg('参数错误', 0, 'site.php');
					return false;
				}
			}
			else
			{
				$this->get['id'] = 1;
			}
			if( ! isset($this->get['title']) )
			{
				$this->get['title'] = '新闻';
			}
			return true;
		}

		function tool_map( $data, $cate_data )
		{
			$ret = array();
			$old_parent_cname = '';
			foreach($data as $row)
			{
				$cnt = 0;
				foreach ( $cate_data as $value )
				{
					if ( intval( $value['site_cate_id'] ) == intval( $row['id'] ) )
					{
						$cnt = $value['cnt'];
					}
				}
				$ret[$row['parent_cname']][] = array('id'=>$row['id'], 'cname'=>$row['cname'], 'cnt' => $cnt );
				$old_parent_cname = $row['parent_cname'];
			}

			return $ret;
		}

	}

	$action = new action();
	$action->run();
?>